package com.yunc.base.queue.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunc.modules.shop.entity.ShopRandomCode;
import com.yunc.modules.shop.service.IShopRandomCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.concurrent.CountDownLatch;

/**
 * 接受消息
 *
 * @author taocongcong
 * @create 2020-08-14 10:48
 */
@Component
public class MsgReceiver {

    private static final Logger logger = LoggerFactory.getLogger(MsgReceiver.class);

    @Autowired
    private IShopRandomCodeService shopRandomCodeService;

    private CountDownLatch latch;

    @Autowired
    public MsgReceiver(CountDownLatch latch) {
        this.latch = latch;
    }

    public void receiveMessage(String str) {
        logger.info("消息接收:" + str);
        Jackson2JsonRedisSerializer seria = new Jackson2JsonRedisSerializer(ShopRandomCode.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        seria.setObjectMapper(objectMapper);
        ShopRandomCode  shopRandomCode= (ShopRandomCode)seria.deserialize(str.getBytes());
        shopRandomCodeService.save(shopRandomCode);
        latch.countDown();
    }

}
